package com.novanok.googledocxlstest.dao;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.novanok.googledocxlstest.Comun.Comun;
import com.novanok.googledocxlstest.entidad.Cliente;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.Serializable;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

public class DAODispositivo {

    private static final String TAG = "DAODispositivo";

    public void guardar(Serializable save, Activity act){
        try {
            FileOutputStream fos = act.openFileOutput(Comun.getArchivo(), Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(save);
            os.close();

            Log.d(TAG, "Datos guardados en Dispositivo");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Serializable cargar(Activity act){
        try {
            Log.d(TAG, "Leyendo datos del Dispositivo");

            FileInputStream fis = act.openFileInput( Comun.getArchivo() );
            ObjectInputStream is = new ObjectInputStream( fis );
            ArrayList<Cliente> simpleClass = (ArrayList<Cliente>) is.readObject();
            is.close();

            return simpleClass;

        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
        } catch ( OptionalDataException e ) {
            e.printStackTrace();
        } catch ( StreamCorruptedException e ) {
            e.printStackTrace();
        } catch ( IOException e ) {
            e.printStackTrace();
        }

        return null;
    }
}
